from django.conf import settings
from django.http import HttpResponse
from django.urls import reverse
from django.utils.safestring import mark_safe
from django.db import transaction

from stark.service import stark
from stark.service.stark import StarkHandler, Option


class CustomerConfig(StarkHandler):
    def display_record(self, obj=None, is_header=None, *args, **kwargs):
        if is_header:
            return '跟进记录'
        tpl = '<a href="%s?cid=%s">跟进记录</a>' % (reverse('stark:app01_consultrecord_list'),obj.id)
        return mark_safe(tpl)

    list_display = ['name', 'qq', stark.get_choice_text('状态', 'status'), stark.get_choice_text('性别', 'gender'),
                    stark.get_choice_text('客户来源', 'source'), stark.get_m2m_text('课程', 'course'), 'consultant',display_record]

    search_group = [
        Option('status'),
        Option('source'),
    ]
    search_list = ['name__contains', 'course__name__contains']

    def get_list_display(self, request, *args, **kwargs):
        val = super().get_list_display(request)
        val.insert(0, StarkHandler.display_checkbox)
        return val


class PublicCustomerConfig(CustomerConfig):
    search_group = [
        Option('source'),
    ]

    def get_queryset(self, request, *args, **kwargs):
        return self.model_class.objects.filter(consultant__isnull=True)

    def get_list_display(self, request, *args, **kwargs):
        val = super().get_list_display(request)
        val.remove(StarkHandler.display_del)
        return val

    def multi_apply(self, request):
        """
        批量申请客户
        :return:
        """
        customer_list = request.POST.getlist('pk')
        user_id = request.session.get('user_info')['id']
        customer_count = self.model_class.objects.filter(consultant_id=user_id, status=2).count()
        if customer_count + len(customer_list) > settings.MAX_CUSTOMER_COUNT:
            return HttpResponse('不要太贪心哟')
        flag = False
        with transaction.atomic():
            query = self.model_class.objects.filter(id__in=customer_list, consultant__isnull=True).select_for_update()
            if len(customer_list) == len(query):
                flag = True
                query.update(consultant_id=user_id)
        if not flag:
            return HttpResponse('手速太慢，已被其他顾问申请')

    multi_apply.text = '申请客户'

    action_list = [multi_apply]


class PrivateCustomerConfig(CustomerConfig):
    def display_record(self, obj=None, is_header=None, *args, **kwargs):
        if is_header:
            return '跟进记录'
        tpl = '<a href="%s?cid=%s">跟进记录</a>' % (reverse('stark:app01_consultrecord_private_list'),obj.id)
        return mark_safe(tpl)

    list_display = ['name', 'qq', stark.get_choice_text('状态', 'status'), stark.get_choice_text('性别', 'gender'),
                    stark.get_choice_text('客户来源', 'source'), stark.get_m2m_text('老师', 'course'),display_record]

    def get_queryset(self, request, *args, **kwargs):
        user_id = request.session.get('user_info')['id']
        return self.model_class.objects.filter(consultant_id=user_id)

    def multi_remove(self, request):
        """
            批量移除客户
            :return:
        """
        customer_list = request.POST.getlist('pk')
        user_id = request.session.get('user_info')['id']
        query = self.model_class.objects.filter(id__in=customer_list, consultant_id=user_id, status=2)
        query.update(consultant_id=None)

    multi_remove.text = '移除客户'
    action_list = [multi_remove]

    # 移除私户的删除功能
    def get_list_display(self, request, *args, **kwargs):
        val = super().get_list_display(request)
        val.remove(StarkHandler.display_del)
        return val
